package com.chen.until;


import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

public class MyUntils {

    public static String returnId() {
        return UUID.randomUUID().toString().replace("-", "");
    }


    public static String  getShortId(){
        String replace = UUID.randomUUID().toString().replace("-", "");
        return replace.substring(0,6);
    }

    public static String returnTime() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        return formatter.format(currentTime);
    }




    //格式化时间  将20200902 格式为 2020-08-15
    public static String returnFormartTime(String time) throws ParseException {
        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatter2 = new SimpleDateFormat("yyyyMMdd");
        return time = formatter1.format(formatter2.parse(time));
    }


    //添加时间 将时间输入，即可返回添加完成后的时间
    public static String addTime(String time, int days) throws ParseException {
        DateFormat format= new SimpleDateFormat("yyyyMMdd");
        Date date=format.parse(time);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);//设置起时间
        cal.add(Calendar.DATE, days);//增加几天
        return format.format(cal.getTime());
    }


    public static  String formartTime(String time){
        if( !"".equals(time) ){
              time = time.replace("/","");
              return  time;
        }else {
            return "";
        }
    }


    private static final String X_FORWARDED_FOR = "x-forwarded-for";

    public static String returnIpAddr(HttpServletRequest request) {
        String ip = request.getHeader(X_FORWARDED_FOR);
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
